<template>
  <div>
    <van-nav-bar title="黑马头条" fixed placeholder />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.mobile"
        :name="form.mobile"
        label="手机号"
        placeholder="手机号"
        required
        :rules="[
          { required: true, message: '请填写手机号', pattern: /^1[3-9]\d{9}$/ },
        ]"
      />
      <van-field
        v-model="form.code"
        type="password"
        :name="form.code"
        label="密码"
        placeholder="密码"
        required
        :rules="[{ required: true, message: '请填写密码', pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :disabled="isLoading"
          :loading="isLoading"
          loading-text="拼命登陆中"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLoginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {
  data () {
    return {
      form: {
        mobile: '13888888888',
        code: '246810'
      },
      isLoading: false
    }
  },
  methods: {
    async onSubmit () {
      this.isLoading = true
      try {
        const res = await userLoginAPI({ ...this.form })
        console.log(res)
        this.isLoading = false
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '登陆成功', duration: 2000 })
        this.$router.push('/layout')
      } catch (err) {
        console.log(err)
        this.isLoading = false
        Notify({ type: 'danger', message: '手机号或密码输入错误', duration: 2000 })
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
